package com.testing.class1;

import com.google.common.eventbus.DeadEvent;
import com.testing.common.ExcelWriter;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname TestLagou
 * @Description 类型说明
 * @Date 2022/8/25 15:46
 * @Created by 特斯汀Roy
 */
public class TestLagou {
    public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException {
        System.setProperty("webdriver.chrome.driver","DriverExe\\chromedriver.exe");
        ChromeOptions ope=new ChromeOptions();
        ope.addArguments("--user-data-dir=D:\\chromeData\\copyData");
        WebDriver driver =new ChromeDriver(ope);
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.manage().window().maximize();
        driver.get("https://lagou.com");
//        driver.findElement(By.cssSelector("[data-city='深圳']")).click();
        driver.findElement(By.className("resume-remind-close")).click();
        driver.findElement(By.id("search_input")).sendKeys("软件测试");
        driver.findElement(By.id("search_button")).click();
        Thread.sleep(1000);
        driver.findElement(By.xpath("//div[@class='option__2xJt5' and text()='长沙']")).click();
        Thread.sleep(4000);
        String pageNumStr = driver.findElement(By.xpath("//li[@title='下一页']/preceding-sibling::li[1]")).getAttribute("title");
        int pNum = Integer.parseInt(pageNumStr);
        List<List<String>> details = new ArrayList<>();
        for (int x = 1; x < pNum; x++) {
            Thread.sleep(3000);
            List<WebElement> elements = driver.findElements(By.cssSelector(".list__YibNq .item__10RTO .p-top__1F7CL a"));
            String homepage = driver.getWindowHandle();
            for (WebElement element : elements) {
                element.click();
                for (String handle : driver.getWindowHandles()) {
                    if (!handle.equals(homepage)) {
                        driver.switchTo().window(handle);
                        break;
                    }
                }
                List<String> onejob = new ArrayList<>();
                String salary = driver.findElement(By.className("salary")).getText();
                String detail = driver.findElement(By.className("job-detail")).getText();
                String company = driver.findElement(By.className("company")).getText();
                System.out.println("公司：" + company);
                System.out.println("薪资" + salary);
                System.out.println("详情:" + detail);
                onejob.add(company);
                onejob.add(salary);
                onejob.add(detail);
                details.add(onejob);
                System.out.println("===============================================");
                driver.close();
                driver.switchTo().window(homepage);
            }
            driver.findElement(By.xpath("//li[@title='下一页']/a")).click();
        }
            System.out.println(details);
            ExcelWriter writer = new ExcelWriter("D:\\拉勾招聘.xlsx");
            writer.writeLine(0, new ArrayList<String>() {{
                add("公司");
                add("薪资");
                add("描述");
            }});
            for (int i = 0; i < details.size(); i++) {
                writer.writeLine(i + 1, details.get(i));
            }
            writer.save();


    }
}
